package com.wctj.api.business.common.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wctj.api.base.entity.DdwxMember;
import com.wctj.api.base.utils.InstanceConvertUtil;
import com.wctj.api.business.common.dao.DdwxMemberMapper;
import com.wctj.api.business.common.dto.DdwxMemberDTO;
import com.wctj.api.business.common.service.DdwxMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class DdwxMemberServiceImpl extends ServiceImpl<DdwxMemberMapper, DdwxMember> implements DdwxMemberService {

    private static final Logger logger = LoggerFactory.getLogger(DdwxMemberServiceImpl.class);

    @Autowired
    private DdwxMemberMapper ddwxMemberMapper;


    @Override
    public DdwxMemberDTO findById(Long id) {
        DdwxMember ddwxMember = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(DdwxMember::getId, id)
                .one();
        return InstanceConvertUtil.convertObject(ddwxMember, DdwxMemberDTO.class);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateBalanceByCmbRecharge(DdwxMemberDTO ddwxMemberDTO, String amount) {
        BigDecimal money = ddwxMemberDTO.getMoney();
        BigDecimal afterMoney = new BigDecimal(amount).add(money);
        logger.info("用户余额发生变动 mid = {}, money = {}, afterMoney = {}", ddwxMemberDTO.getId(), money, afterMoney);
        ddwxMemberDTO.setMoney(afterMoney);
        DdwxMember ddwxMember = InstanceConvertUtil.convertObject(ddwxMemberDTO, DdwxMember.class);
        ddwxMemberMapper.updateById(ddwxMember);
    }
}
